# $Id: models.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.db import models
from djanet.fwrules import models as fwmodels
from djanet.lib import exception, stdlib
from djanet.db import model, netdb, choices

__version__ =  "$Revision: 7557b5d4d82b $"
__author__ = "$Author: pgurumur $"

# Create your models here.
class DiffieHellman(model.BaseModel):
   group_name = models.CharField(blank = True, null = True, max_length = 20,
         choices = choices.DH_CHOICES,
         help_text = "Diffie Hellman group name")
   group_size = models.IntegerField(blank = True, null = True, default = 0,
         help_text = "Diffie Hellman group size")

   class Meta:
      db_table = u'dh_group'

   def __str__(self):
      return stdlib.my_str(self.group_name)

class CryptoTransforms(model.BaseModel):
   cipher_name = models.CharField(max_length = 20,
         choices = choices.CIPHER_CHOICES, help_text = "Cipher transforms")
   auth_name = models.CharField(max_length = 20,
         choices = choices.AUTH_CHOICES, help_text = "Cipher transforms")

   class Meta:
      db_table = u'crypto_transforms'

   def __str__(self):
      return "(%s %s)" %(self.cipher_name, self.auth_name)

class MainModePolicy(model.BaseModel):
   name = models.CharField(max_length = 24,
         help_text = "Name of main mode policy")
   mm_auth = models.CharField(max_length = 50,
         choices = choices.MM_AUTH_CHOICES,
         help_text = "Main mode authentication choices")
   transforms = models.ForeignKey(CryptoTransforms)
   dh_group = models.ForeignKey(DiffieHellman)
   lifetime = models.IntegerField(default = 86400,
         help_text = "Lifetime of main mode policy")

   class Meta:
      db_table = u'mm_policy'

   def save(self, *args, **kwargs):
      if self.lifetime > 86400:
         raise exception.ModelError, "lifetime cannot be greater than 1 day"

      super(MainModePolicy, self).save(*args, **kwargs)

   def __str__(self):
      return stdlib.my_str(self.name)

class QuickModePolicy(model.BaseModel):
   name = models.CharField(max_length = 24,
         help_text = "Name of the quick mode policy")
   transforms = models.ForeignKey(CryptoTransforms)
   pfs = models.ForeignKey(DiffieHellman, null = True, blank = True)
   lifetime = models.IntegerField(default = 28800,
         help_text = "Lifetime of quick mode policy")
   nat_t = models.BooleanField(help_text = "NAT Traversal")
   acl = models.ForeignKey(fwmodels.IPAccessList)
   in_spi = models.IntegerField(default = 0, null = True, blank = True,
         help_text = "Inbound SPI value for quick mode")
   out_spi = models.IntegerField(default = 0, null = True, blank = True,
         help_text = "Outbound SPI value for quick mode")
   in_cipher = models.CharField(max_length = 75, null = True, blank = True,
         help_text = "Inbound Cipher value for quick mode")
   out_cipher = models.CharField(max_length = 75, null = True, blank = True,
         help_text = "Outbound Cipher value for quick mode")
   in_auth = models.CharField(max_length = 75, null = True, blank = True,
         help_text = "Inbound Auth value for quick mode")
   out_auth = models.CharField(max_length = 75, null = True, blank = True,
         help_text = "Outbound Auth value for quick mode")

   class Meta:
      db_table = u'qm_polcy'

   def __str__(self):
      return stdlib.my_str(self.name)

class CryptoMap(model.BaseModel):
   name = models.CharField(max_length = 24,
         help_text = "Name of the crypto map")
   sequence = models.IntegerField(help_text = "Crypto map sequence number")
   device = models.ForeignKey(netdb.Device)
   interface = models.ForeignKey(netdb.Interface)
   quickmode = models.ForeignKey(QuickModePolicy)

   class Meta:
      db_table = u'crypto_map'

   def __str__(self):
      return stdlib.my_str(self.name)

class IPSec(model.BaseModel):
   local_peer = models.ForeignKey(netdb.Device,
         help_text = "Local VPN concentrator")
   remote_peer = model.IPAddressField(unique = True,
         help_text = "Remote VPN concentrator")
   crypto_map = models.ForeignKey(CryptoMap)
   ikepolicy = models.ForeignKey(MainModePolicy, null = True, blank = True)
   psk = models.CharField(null = True, blank = True,
         unique = True, max_length = 75,
         help_text = "Pre shared key for authentication")

   class Meta:
      db_table = u'ipsec'

   def __str__(self):
      return "(%s, %s)" %(self.local_peer, self.remote_peer)

